package com.heima.file.config;

import com.heima.file.properties.MinIoProperties;
import com.heima.file.template.MinIOTemplate;
import io.minio.MinioClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(MinioClient.class)
@EnableConfigurationProperties(MinIoProperties.class)
public class MinIoAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(MinIOTemplate.class)
    public MinIOTemplate minIoTemplate(MinioClient minioClient, MinIoProperties minIoProperties){

        return new MinIOTemplate(minioClient,minIoProperties);
    }

    @Bean
    @ConditionalOnMissingBean
    public MinioClient buildMinioClient(MinIoProperties minIoProperties) {
        return MinioClient.builder()
                .credentials(minIoProperties.getAccessKey(), minIoProperties.getSecretKey())
                .endpoint(minIoProperties.getEndpoint())
                .build();
    }
}
